/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projectmangement;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author trungquy
 */
public class Project {

    private int id;
    private String name;
    private Date start_date;
    private Date end_date;
    private int left;
    private int right;
    
    
    private DefaultMutableTreeNode node;
    private int depth;
    public Project() {
        id = 0;
        name = "";
        start_date = new Date();
        end_date = new Date();
        left = 0;
        right = 0;
        node = null;
        depth = -1;
    }

    public int get_id() {
        return id;
    }

    public void set_id(int _id) {
        id = _id;
    }

    public String get_name() {
        return name;
    }

    public void set_name(String _name) {
        name = _name;
    }

    public Date get_start_date() {
        return start_date;
    }

    public void set_start_date(Date _start_date) {
        start_date = _start_date;
    }

    public Date get_end_date() {
        return end_date;
    }

    public void set_end_date(Date _end_date) {
        end_date = _end_date;
    }

    public void set_(Date _end_date) {
        end_date = _end_date;
    }

    public int get_left() {
        return left;
    }

    public void set_left(int _left) {
        left = _left;
    }

    public void set_right(int _right) {
        right = _right;
    }

    public int get_right() {
        return right;
    }
    
    public int get_depth() {
        if(depth < 0){
            Project_Dao dao = new Project_Dao();
            depth = dao.getDepthOfNode(this);
        }
        return depth;
    }

    public void set_depth(int _depth) {
        depth = _depth;
    }
    
    public DefaultMutableTreeNode getTreeNodeObj(){
        if(node == null){
            node = new DefaultMutableTreeNode(this);
        }
        return node;
    }
    
    public int getDepthFromRoot(){
        Project_Dao dao = new Project_Dao();
        return dao.getDepthOfNode(this);
    }
    
    public void initialDirectChildNodes(){
        node = new DefaultMutableTreeNode(this);
        Project_Dao dao = new Project_Dao();
        ArrayList<Project> projects = dao.selectAllDirectChilds(this);
        Iterator<Project> it = projects.iterator();
        while(it.hasNext()){
            Project project = it.next();
            DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(project);
            node.add(childNode);
        }
    }
    
    public ArrayList<Project> getAllChilds(){
        Project_Dao dao = new Project_Dao();
        ArrayList<Project> projects = dao.selectAllChilds(this);
        return projects;
    }
    public ArrayList<Project> getAllDirectChilds(){
        Project_Dao dao = new Project_Dao();
        ArrayList<Project> projects = dao.selectAllDirectChilds(this);
        return projects;
    }
    @Override
    public String toString(){
        return this.name;
    }
    public static ArrayList<Project> selectRootNodes(){
        Project_Dao dao = new Project_Dao();
        return dao.selectRootNodes();
    }    
}
